package com.baway.biliclitent.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.baway.biliclitent.R;
import com.baway.biliclitent.model.Details_Recom_Bean;
import com.baway.biliclitent.view.LiveGridView;
import com.bumptech.glide.Glide;


import java.util.List;

/**
 * 1.类的用途：详情界面番剧推荐的适配器
 * 2.渠刚
 * 3.@date
 * 4.用途:
 */
public class MyRecoAdapter extends RecyclerView.Adapter<MyRecoAdapter.MyViewHolder>{
    private Context context;
    private List<Details_Recom_Bean.ResultBean.ListBean> dataList;
    public MyRecoAdapter(Context context) {
        this.context = context;
    }
    //设置数据
    public void setData(List<Details_Recom_Bean.ResultBean.ListBean> dataList){
        this.dataList = dataList;
        this.notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_details_layout, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        MyBaseAdapter myBaseAdapter = new MyBaseAdapter(context,dataList);
        holder.myGrid.setAdapter(myBaseAdapter);
    }

    @Override
    public int getItemCount() {
        return dataList==null?0:1;
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private final LiveGridView myGrid;

        public MyViewHolder(View itemView) {
            super(itemView);
            myGrid = (LiveGridView) itemView.findViewById(R.id.details_gridView);
        }
    }
    class MyBaseAdapter extends BaseAdapter{
        private Context context;
        private List<Details_Recom_Bean.ResultBean.ListBean> dataList;


        public MyBaseAdapter(Context context, List<Details_Recom_Bean.ResultBean.ListBean> dataList) {
            this.context = context;
            this.dataList = dataList;
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int i) {
            return dataList.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder = null;
            if(view == null){
                holder = new ViewHolder();
                view = LayoutInflater.from(context).inflate(R.layout.item_details_reco_layout,null);
                holder.detailsImg = (ImageView) view.findViewById(R.id.details_image);
                holder.favoriteTv = (TextView) view.findViewById(R.id.details_favorite_index);
                holder.detailsTitleTv = (TextView) view.findViewById(R.id.details_nameTv);
                view.setTag(holder);
            }else{
                holder = (ViewHolder) view.getTag();
            }
            holder.detailsTitleTv.setText(dataList.get(i).getTitle());
            holder.favoriteTv.setText(dataList.get(i).getFollow()+"人在追番");
            Glide.with(context).load(dataList.get(i).getCover()).into(holder.detailsImg);
            return view;
        }
        class ViewHolder{
            ImageView detailsImg;
            TextView detailsTitleTv,favoriteTv;
        }
    }
}
